import Axios from "axios";
import { isDev } from "../../config";

let apiOrigin = isDev ? "https://t2.hqs.link" : "http://127.0.0.1:1602";

export const axios = Axios.create({
  baseURL: apiOrigin + "/app/22",
});

function doPost(url, body) {
  return axios.post(url, body).then((res) => res.data);
}
function getRow(url, params) {
  if (params > -1) {
    params = { id: params };
  }
  return axios.get(url, { params }).then((res) => res.data.row);
}
function getList(url, params) {
  return axios.get(url, { params }).then(({ data }) => {
    if (params._limit === 0) return data.count;
    if (params._data) return data;
    return data.rows;
  });
}

export const api = {
  getData(type, tab, body) {
    const path = `/data/${type}/${tab}`;
    if (type == "list") {
      body._data = 1;
      return getList(path, body);
    }
    if (type == "item") return getRow(path, body);
    if (type == "upsert") return doPost(path, body);
    throw "Invalid type:" + type;
  },
  postUserInfo(body) {
    return doPost("/data/upsert/user-info", body);
  },
  getUserInfo(params) {
    return getRow("/data/item/user-info", params);
  },
  getUserList(params) {
    return getList("/data/list/user-info", params);
  },
  getUserInfoBase(params) {
    return getRow("/data/item/user-info-base", params);
  },
  postUserInfoBase(body) {
    return doPost("/data/upsert/user-info-base", body);
  },

  getMembers(params) {
    return getList("/data/list/member", params);
  },
  getMember(params) {
    return getRow("/data/item/member-adm", params);
  },
  postMember(body) {
    return doPost("/data/upsert/member", body);
  },

  getShops(params, path) {
    return getList(path || "/data/list/shop", params);
  },
  getShopRow(params, path) {
    return getRow(path || "/data/item/shop", params);
  },
  postShop(body) {
    return doPost("/data/upsert/shop", body);
  },
  getBannerList(params) {
    return getList("/data/list/banner", params);
  },

  getGoodsList(params, path) {
    return getList(path || "/data/list/goods", params);
  },
  getGoodsRow(params) {
    return getRow("/data/item/goods", params);
  },
  postGoods(body) {
    return doPost("/data/upsert/goods-ice", body);
  },

  postOrder(body) {
    return doPost("/data/upsert/order-ice", body);
  },
  getOrder(params) {
    return getRow("/data/item/order-ice", params);
  },
  getOrderList(params, path) {
    return getList(path || "/data/list/order-ice", params);
  },

  postOrderLog(body) {
    return doPost("/data/upsert/order-log-ice", body);
  },
  getOrderLogs(params) {
    return getList("/data/list/order-log-ice", params);
  },
  getOrderLog(params) {
    return getRow("/data/item/order-log", params);
  },

  getNfcRows(params) {
    return getList("/data/list/nfc-log", params);
  },
  getNfcRow(params) {
    return getRow("/data/item/nfc-log", params);
  },
  postNfc(body) {
    return doPost("/data/upsert/nfc-log", body);
  },

  async postActLog(body) {
    try {
      await doPost("/data/upsert/user-log", body);
    } catch (error) {
      console.log("act-log-fail", error.message, body);
    }
  },
  getActLogs(params) {
    return getList("/data/list/user-log", params);
  },
};
